//
//  ZJKTextView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/25.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

class ZJKTextView: UITextView {

    // 提供给外界设置占位文字的属性
    var placeholder: String? {
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    // 外界设置了当前textView的字体的话，需要将占位文字的字体一并设置
    override var font: UIFont? {
        didSet{
            placeholderLabel.font = self.font
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(placeholderLabel)
        
        // 添加约束
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(self).offset(-10)
        }
        
       //通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChage", name: UITextViewTextDidChangeNotification, object: nil)
    }
   
    
    @objc private func textDidChage(){
        placeholderLabel.hidden = hasText()
    }
    
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    
    
    
    // MARK: -懒加载控件
    private lazy var placeholderLabel: UILabel = {
        
        let label = UILabel(textColor: UIColor.lightGrayColor(), fontSize: 12)
        
        label.text = "听说下雨榴莲与音乐更配哟～"
        
        label.numberOfLines = 0
        
        return label
    }()

}
